package com.shmilyou.dissertationmanagement.resolver;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2018/11/23 */

import com.shmilyou.dissertationmanagement.utils.Constant;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType() == LoginUser.class;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {
        return Objects.requireNonNull(nativeWebRequest
                .getNativeRequest(HttpServletRequest.class))
                .getSession()
                .getAttribute(Constant.LOGIN_USER);
    }
}
